//
//  WDBusinessListController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/20.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDBusinessListController.h"
#import "WDBusiness.h"
#import "WDFirstSort.h"

@interface WDBusinessListController () <UITableViewDelegate, UITableViewDataSource>
/** 商户数据 */
@property (nonatomic, strong) NSArray *businessArr;
/** 商户排序数据 */
@property (nonatomic, strong) NSArray *dataArr;
/** tableView */
@property(nonatomic, weak) UITableView *businessTableView;

@end

@implementation WDBusinessListController

- (NSArray *)businessArr
{
    WDSQLiteManager *manager = [WDSQLiteManager manager];
    
    _businessArr = [manager selectWithTable:NSStringFromClass([WDBusiness class])];
    
    return _businessArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadNavItemWithAction:@selector(NavBackBtn) btnImage:@"icon_back" btnSelImage:@"icon_back" title:@"商户列表"];
    
    //排序商户
    self.businessArr = [self.businessArr sortedArrayUsingComparator:^NSComparisonResult(WDBusiness *obj1, WDBusiness *obj2) {
        return [obj1.start_with compare:obj2.start_with];
    }];
    
    //商户按首字母分组字典数组
    self.dataArr = [WDFirstSort firstSortWithObjectArray:self.businessArr];
    
    [self setUpTableView];
}

- (void)NavBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpTableView
{
    UITableView *businessTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, WDScreenH - 64) style:UITableViewStylePlain];
    
    businessTableView.delegate = self;
    
    businessTableView.dataSource = self;
    
    //设置索引字体颜色
    businessTableView.sectionIndexColor = ColorForRGB(0xa0a0a0);
    
    //设置索引背景颜色
    businessTableView.sectionIndexBackgroundColor = [UIColor clearColor];
    
    businessTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:businessTableView];
    
    self.businessTableView = businessTableView;
    
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dict = self.dataArr[section];
    
    NSMutableArray *array = dict[@"content"];
    
    return array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    
    UITableViewCell *cell;
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
        NSDictionary *dict = self.dataArr[indexPath.section];
        
        NSArray *businessArr  = dict[@"content"];
        
        WDBusiness *tempBusiness = businessArr[indexPath.row];
        
        cell.textLabel.text = tempBusiness.name;
        
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        
    }
    
    return cell;
}

#pragma mark - <UITableViewDelegate>
//把选中行传到上个控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = self.dataArr[indexPath.section];
    NSArray *businessArr  = dict[@"content"];
    
    WDBusiness *business = businessArr[indexPath.row];
    
    //用block把商户信息传过去
    self.businessName(business.name, business.id);
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *resultArray = [NSMutableArray array];
    for (NSDictionary *dict in self.dataArr) {
        NSString *title = dict[@"firstLetter"];
        [resultArray addObject:title];
    }
    return resultArray;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, 20)];
    headView.backgroundColor = ColorForRGB(0xf0f1f2);
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, WDScreenW, 20)];
    
    NSDictionary *dict = self.dataArr[section];
    NSString *title = dict[@"firstLetter"];
    label.text = title;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = ColorForRGB(0x6b6b6b);
    [headView addSubview:label];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
